// pages/index/nearbyTeams/teamDetail/teamDetail.js
import request from '../../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    servicePac: [],
    team1:{},
    institution:{},
    tags:[
      {name: '基础医疗', class: 'tag-blue' },
      { name: '儿童护理', class: 'tag-yellow' },
      { name: '慢病管理', class: 'tag-green' }
    ],
    team: {
      name: '李明明团队',
      institution: '翡翠郡社区服务站',
      signedCount: 500,
      rating: 4.0,
      tags: ['慢病管理', '中医服务', '老年人管理'],
      doctors: [
        {
          id: 1,
          name: '李明明',
          title: '全科医师',
          isLeader: true,
          signedCount: 45,
          avatar: '/assets/images/doctor-avatar.png'
        },
        {
          id: 2,
          name: '王小倩',
          title: '全科护士',
          isLeader: false,
          signedCount: 45,
          avatar: '/assets/images/doctor-avatar.png'
        },
        {
          id: 3,
          name: '高岭',
          title: '护理医师',
          isLeader: false,
          signedCount: 45,
          avatar: '/assets/images/doctor-avatar.png'
        }
      ]
    }
  },
  onDoctorClick(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      //serviceDetail/serviceDetail?id=${id}
      url: `/pages/index/nearbyTeams/doctorDetail/doctorDetail?id=${id}`
    });
  },
  onServiceClick(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      //serviceDetail/serviceDetail?id=${id}
      url: `/pages/index/nearbyTeams/serviceDetail/serviceDetail?id=${id}`
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 通过options.id获取团队详情
    this.getTeamDetail(options.id);
    this.getDoc(options.id);
    this.getServicePac();
  },

  getTeamDetail(id) {
    request({
      url: 'system/team/'+id,
      method: 'GET',
      success: (result) => {
        this.setData({
          team1:result.data.data
        })
        request({
          url: 'system/institution/'+result.data.data.institutionid,
          method: 'GET',
          success: (res) => {
            this.setData({
              institution:res.data.data
            })
          },
          fail: (err) => {},
          complete: (res) => {},
        })
      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  getDoc(teamid){
    request({
      url: 'sysmanagement/doctorteam/byteamid/'+teamid,
      method: 'GET',
      success: (result) => {
        console.log(result.data.data[0].doctorList)
        this.setData({
          doctors:result.data.data[0].doctorList
        })
      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  getServicePac() {
    request({
      url: 'package/package/list',
      method: "GET",
      success: (result) => {
        console.log(result.data.rows);
        this.setData({
          servicePac: result.data.rows
        });

      },
      fail: (err) => {
        console.error('获取服务包失败', err);
        wx.showToast({
          title: '获取服务包失败',
          icon: 'none'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onDoctorSign(e) {
    const doctorId = e.currentTarget.dataset.doctorid;
    wx.showModal({
      title: '提示',
      content: '确定要签约该医生吗？',
      success: (res) => {
        if (res.confirm) {
          // 处理签约逻辑
          request({
            url: 'your-sign-api-endpoint',
            method: 'POST',
            data: {
              doctorId: doctorId,
              // 其他需要的参数
            },
            success: (result) => {
              wx.showToast({
                title: '签约成功',
                icon: 'success'
              });
              // 刷新医生列表
              this.getDoc(this.data.team1.id);
            },
            fail: (err) => {
              wx.showToast({
                title: '签约失败',
                icon: 'error'
              });
            }
          });
        }
      }
    });
  },

  onTeamSign() {
    // 处理团队签约
    wx.showModal({
      title: '提示',
      content: '确定要签约该团队吗？',
      success: (res) => {
        if (res.confirm) {
          // 处理签约逻辑
        }
      }
    });
  }
})